/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

import com.pearreview.ui.*;
import com.pearreview.application.Messages;


public class CatalogControlPanel extends JPanel {

	private static final long serialVersionUID = 5932969672393831692L;
	private FormLabel headingWokspace;
	private FormLabel headingList;
	
	private JButton importBtn;
	private JButton exportBtn;
	private JButton createAspectBtn;
	private JButton createCategoryBtn;
	private JButton createCatalogBtn;
	private JButton removeBtn;
	
	private Action importAction;
	private Action exportAction;
	private Action addCatalogAction;
	private Action addCategoryAction;
	private Action addAspectAction;
	private Action removeAction;
	
	public CatalogControlPanel(
			Action importAction,
			Action exportAction,
			Action addCatalogAction,
			Action addCategoryAction,
			Action addAspectAction,
			Action removeAction) {
		
		super();
		
		this.importAction = importAction;
		this.exportAction = exportAction;
		this.addCatalogAction = addCatalogAction;
		this.addCategoryAction = addCategoryAction;
		this.addAspectAction = addAspectAction;
		this.removeAction = removeAction;
		
		initComponents();
	}

	private void initComponents() {

		headingWokspace = new FormLabel();
		headingList = new FormLabel();
		importBtn = new JButton();
		exportBtn = new JButton();
		createAspectBtn = new JButton();
		createCategoryBtn = new JButton();
		createCatalogBtn = new JButton();
		removeBtn = new JButton();

		//======== this ========
		setOpaque(false);

		//---- headingWokspace ----
		headingWokspace.setText(Messages.getString(
				"PearReview.Moderator.aspects.workspace"));
		headingWokspace.setFont(new Font("Tahoma", Font.PLAIN, 14));

		//---- headingList ----
		headingList.setText(Messages.getString(
				"PearReview.Moderator.aspects.review_aspects"));
		headingList.setFont(new Font("Tahoma", Font.PLAIN, 14));

		//---- importBtn ----
		importBtn.setAction(importAction);
		importBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- exportBtn ----
		exportBtn.setAction(exportAction);
		exportBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createAspectBtn ----
		createAspectBtn.setAction(addAspectAction);
		createAspectBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createCategoryBtn ----
		createCategoryBtn.setAction(addCategoryAction);
		createCategoryBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createCatalogBtn ----
		createCatalogBtn.setAction(addCatalogAction);
		createCatalogBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		//---- createRemoveBtn ----
		removeBtn.setAction(removeAction);
		removeBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		ArrayList<JComponent> workspaceList = new ArrayList<JComponent>();
		workspaceList.add(importBtn);
		workspaceList.add(exportBtn);
		calcPreferredSize(workspaceList);
		
		ArrayList<JComponent> reviewList = new ArrayList<JComponent>();
		reviewList.add(createAspectBtn);
		reviewList.add(createCategoryBtn);
		reviewList.add(createCatalogBtn);
		reviewList.add(removeBtn);
		calcPreferredSize(reviewList);

		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addComponent(headingList, 
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(createCatalogBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(createAspectBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(createCategoryBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(removeBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(50)
					.addGroup(layout.createParallelGroup()
						.addComponent(headingWokspace, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(importBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(exportBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(40))
		);
		
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(headingWokspace, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(headingList, GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(20)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addComponent(importBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(exportBtn))
						.addGroup(layout.createSequentialGroup()
							.addComponent(createAspectBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(createCategoryBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(createCatalogBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(removeBtn)))
					.addContainerGap(20, Short.MAX_VALUE))
		);
	}
	
	private Dimension calcPreferredSize(ArrayList<JComponent> list) {
		Dimension biggestDim = new Dimension(1,1);
		for (JComponent comp: list) {
			Dimension currentDim = comp.getPreferredSize();
			if (currentDim.height > biggestDim.height)
				biggestDim.height = currentDim.height;
			if (currentDim.width > biggestDim.width)
				biggestDim.width = currentDim.width;
		}
		
		for (JComponent comp: list) {
			comp.setPreferredSize(biggestDim);
		}
		
		return biggestDim;
	}
}
